﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
using WHelper = PGMHelper.WilsonHelper;
using PGMHelper;

namespace HystereticBasic
{
    public partial class HystereticForm : Form
    {
        /// <summary>
        /// 数据点数据
        /// </summary>
        private HyDataSet DATAS { set; get; }

        /// <summary>
        /// 软化骨架定义方法
        /// </summary>
        private SkeletonSoftenMethod skeletonMethod
        {
            get
            {
                if (this.EnvelopeRadioButton.Checked) return SkeletonSoftenMethod.Envelope;
                else if (this.ModEnvelopeRadioButton.Checked) return SkeletonSoftenMethod.ModEnvelope;
                else return SkeletonSoftenMethod.SecantAllCycle;
            }
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        public HystereticForm()
        {
            InitializeComponent();
            //初始化
            this.DATAS = new HyDataSet();
            //外观
            HystereticHelper.SetChartApperance(this.SkeletonChart);
            HystereticHelper.SetChartApperance(this.NumericalSkeletonChart);
            HystereticHelper.SetChartApperance(this.HystereticSeparatedChart);
            HystereticHelper.SetChartApperance(this.SmoothCurveChart);
            HystereticHelper.SetChartApperance(this.NumericalCurveChart);
            //Tab外观
            this.BasicTabControl.SetApperance();
            //DataGridView外观
            this.BasicDataView.SetApperance(DataGridViewSelectionMode.ColumnHeaderSelect);
            this.CircleDataView.SetApperance(DataGridViewSelectionMode.ColumnHeaderSelect);
        }

        /// <summary>
        /// 浏览文件夹
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Folder_Browser(object sender, EventArgs e)
        {
            //初始化文件夹信息
            string directPath = string.Empty;
            //选择文件夹
            if (!WHelper.OpenDirectory(ref directPath)) return;
            //修改TextBox信息
            this.DirectPathTextbox.Text = directPath;
        }

        /// <summary>
        /// 双击文件名绘图
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FileNameListBox_DoubleClick(object sender, EventArgs e)
        {
            //获得选择对象
            var data = (BasicData)this.FileNameListBox.GetSelectObject();
            //绘制曲线
            data.ToSkeletonChart(this.SkeletonChart);
            data.ToNumericalSkeletonChart(this.NumericalSkeletonChart);
            data.ToSmoothCyclesChart(this.SmoothCurveChart);
            data.ToNumericalCyclesChart(this.NumericalCurveChart);
            data.CircleToChart(this.HystereticSeparatedChart, this.CircleDataView, false, false);
            //设定表格数据
            this.BasicDataView.AddDatas(data.GetData());
            //获得单调曲线
            //var mList = data.CircleSet.GetMonotonousList(true);
            //耗能
            //MessageBoxExtern.Asterisk(data.CircleSet.GetEenergy().ToString("f1"));
            //Example.GetCycleMaxDispDemo(data);
        }

        /// <summary>
        /// 数据导出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OutputButton_Click(object sender, EventArgs e)
        {
            //导出Excel
            NPOI.Helper.NPOIExcelHelper.SaveDatasToExcel(this.DATAS.GetGridDataList(), true);
        }

        /// <summary>
        /// 查看详细信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DetailButton_Click(object sender, EventArgs e)
        {
            if (this.DATAS == null)
            {
                MessageBoxExtern.Warnning(string.Format("请先提取数据"));
                return;
            }
            Form form = new DetailForm(this.DATAS);
            form.Show();
        }

        /// <summary>
        /// 计算延性系数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DuctilityButtom_Click(object sender, EventArgs e)
        {
            this.ToDuctilityForm(true);
        }

        /// <summary>
        /// 耗能系数窗体
        /// </summary>
        /// <param name="isPos"></param>
        private void ToDuctilityForm(bool isPos)
        {
            if(this.DATAS.DataList.Count == 0)
            {
                Form form = new DuctilityForm();
                form.Show();
            }
            else
            {
                var objSet = this.DATAS.GetDuctilityObjSet(isPos,
               GlobalParam.UltimateForceFactor, GlobalParam.ParkFactor);
                Form form = new DuctilityForm(objSet);
                form.Show();
            }
        }

        /// <summary>
        /// 负向骨架延性系数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DuctilityNegButtom_Click(object sender, EventArgs e)
        {
            this.ToDuctilityForm(false);
        }

        /// <summary>
        /// 读取数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ReadButton_Click(object sender, EventArgs e)
        {
            //校验登录状态
            if (!Login.LoginState.Check())
                return;
            //所有文件路径
            var filePathList = HystereticHelper.GetFileList(DirectPathTextbox.Text);
            //路径为空
            if (filePathList.Count == 0) return;
            //构造数据
            this.DATAS = new HyDataSet(filePathList, this.skeletonMethod) ;
            //设定ListBox信息：数据绑定
            this.FileNameListBox.SetDataSource(this.DATAS.DataList, "FileName", "FileName");
            MessageBoxExtern.Asterisk("读取成功！");
            //this.DATAS.CircleForceMaxToGrid(this.BasicDataView, this.FilterCheckbox.Checked);
        }
    }
}
